﻿$(document).ready(function () {
    $("#btnMarkasRead").click(function () {
        if ($("#ulUnreadInformation").find("li").length == 0) {
            layer.msg("No unread message!");
            return;
        }

        layer.confirm("Are you sure to mark all of message's status as read?", { icon: 3, title: 'Message', btn: ['Confirm', 'Cancel'] }, function (index) {
            layer.load(2);
            var api_url = window.singleConfig.rootUrl + "api/users/MessageService/markallread";
            $.get(api_url, {}, function (data) {
                layer.closeAll();
                if (data.code == "success") {
                    layer.msg("You had mark " + data.affect_rows + " messages mark as read !");
                    $("#ulUnreadInformation").html("");
                }
                else {
                    layer.msg(data.message);
                }
            }, 'json');
        });
    });

    /*
     * delete notification
     */
    $("[data-type=deleteNotification]").click(function () {
        var id = $(this).attr("data-id");
        var $obj = $(this);

        layer.confirm('Are you sure to delete this message ?', { icon: 3, title: 'Message', btn: ['Confirm', 'Cancel'] }, function (index) {
            layer.load(2);
            var api_url = window.singleConfig.rootUrl + "api/users/MessageService/deleteNotification?id=" + id;
            $.get(api_url, {}, function (data) {
                layer.closeAll();                           
                if (data.code == "success") {
                    layer.msg("Delete successful !");
                    $obj.parents("li:first").remove();
                }
                else {
                    layer.msg(data.message);
                }
            }, 'json');
        });
    });

    /*
     * read notification
     */
    $("[data-type=readNotification]").click(function () {
        var id = $(this).attr("data-id");
        var $obj = $(this);

        layer.confirm('Are you sure to make this message as read ?', { icon: 3, title: 'Message', btn: ['Confirm', 'Cancel'] }, function (index) {
            layer.load(2);
            var api_url = window.singleConfig.rootUrl + "api/users/MessageService/readNotification?id=" + id;
            $.get(api_url, {}, function (data) {
                layer.closeAll();
                if (data.code == "success") {
                    layer.msg("Mark as read successful !");                    
                    if ($obj.parents("ul:first").attr("id") == "ulUnreadInformation") {
                        $obj.parents("li:first").remove();
                    }
                    else {
                        $obj.remove();
                    }
                }
                else {
                    layer.msg(data.message);
                }
            }, 'json');
        });
    });
});